package cn.ligoo.part.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ssm.core.domain.easyui.TreeGrid;
import cn.ligoo.part.domain.SysModAction;
import cn.ligoo.part.domain.SysModule;
import cn.ligoo.part.domain.TypePartCode;
import cn.ligoo.part.domain.UserInfo;
import cn.ligoo.part.service.Facade;

public class ControllerUtils {

	private static final Logger logger = LoggerFactory.getLogger(ControllerUtils.class);

	/**
	 * 递归获取 SysModule列表，用于生成treegrid式数据
	 * 
	 * @param facade
	 * @param sysModule
	 * @param userInfo
	 * @return
	 */
	public static List<SysModule> getSysModuleList(Facade facade, SysModule sysModule, UserInfo userInfo,
			List<SysModAction> sysModActionList) {

		List<SysModule> sysModuleSubList = facade.getSysModuleService().getSysModuleList(sysModule);
		for (SysModule sm : sysModuleSubList) {
			boolean hasPermissions = isHasExecutePermissions(sm, userInfo, sysModActionList);
			if (!hasPermissions) {
				continue;
			}
			sm.getMap().put("isHasPermissions", true);
			sysModule.setMod_pcode(sm.getMod_code());
			sm.setChildren(getSysModuleList(facade, sysModule, userInfo, sysModActionList));
		}
		return sysModuleSubList;
	}

	/**
	 * @param facade
	 * @param sysModule
	 * @see ControllerUtils#getSysModuleList(Facade , SysModule , UserInfo , List<SysModAction>)
	 * @return
	 */
	public static List<SysModule> getSysModuleList(Facade facade, SysModule sysModule) {
		List<SysModule> sysModuleSubList = facade.getSysModuleService().getSysModuleList(sysModule);
		for (SysModule sm : sysModuleSubList) {
			sysModule.setMod_pcode(sm.getMod_code());
			sm.setChildren(getSysModuleList(facade, sysModule));
		}
		return sysModuleSubList;
	}

	public static List<TypePartCode> getTypePartCodeList(Facade facade, TypePartCode typePartCode) {
		List<TypePartCode> typePartCodeSubList = facade.getTypePartCodeService().getTypePartCodeList(typePartCode);
		for (TypePartCode tpc : typePartCodeSubList) {
			// if (!"0".equals(tpc.getType_pcode())) {
			tpc.setState(TreeGrid.EASYUI_TREEGRID_ROW_STATE_OPEN);
			// }
			typePartCode.getRow().setLimit(null);
			typePartCode.getRow().setOffset(null);
			typePartCode.setType_pcode(tpc.getType_code());
			tpc.setChildren(getTypePartCodeList(facade, typePartCode));
		}
		return typePartCodeSubList;
	}

	private static boolean isHasExecutePermissions(SysModule sysModule, UserInfo userInfo,
			List<SysModAction> sysModActionList) {
		logger.debug("..........mod_name:={}", sysModule.getMod_name());
		boolean hasPermissions = false;
		// 如果是根节点，对所有人可见
		if (null != sysModule.getMod_pcode() && sysModule.getMod_pcode() == 0) {
			logger.debug("..........mod_pcode:={}", sysModule.getMod_pcode());
			hasPermissions = true;
		}

		// 如果sys_module表的数据行的is_public=1，不需要判断授权，对所有人可见
		if (null != sysModule.getIs_public() && sysModule.getIs_public() == 1) {
			logger.debug("..........is_public:={}", sysModule.getIs_public());
			hasPermissions = true;
		}
		// 如果用户 是is_admin=1，不需要判断授权，有全部权限
		if (null != userInfo && null != userInfo.getIs_admin() && userInfo.getIs_admin() == 1) {
			logger.debug("..........is_admin:={}", userInfo.getIs_admin());
			hasPermissions = true;
		}

		// 最后一种，就要判断此用户对应的角色代码或用户(目前不支持)对此节点mod_code有无X权限，有无操作权限
		if (null != sysModActionList) {
			for (SysModAction sma : sysModActionList) {
				if (sma.getMod_code() == sysModule.getMod_code()) {
					if (null != sma.getAction_code() && sma.getAction_code().contains("X")) {
						logger.debug("..........contains('X'):={}", true);
						hasPermissions = true;
					}
				}
			}
		}
		logger.debug("----------hasPermissions:={}", new Object[] { hasPermissions });
		return hasPermissions;
	}

	/**
	 * 根据用户来返回用户有权限的操作List
	 * 
	 * @param facade
	 * @param userInfo
	 * @return
	 */
	public static List<SysModAction> getSysModActionList(Facade facade, UserInfo userInfo) {
		if (null == userInfo || null == userInfo.getRole_codes()) {
			return null;
		}
		// 取得用户角色
		String[] role_codes = StringUtils.split(userInfo.getRole_codes(), ",");

		// 根据用户角色取得SysModAction并放到List中
		List<SysModAction> sysModActionList = new ArrayList<SysModAction>();
		if (null != role_codes) {
			for (String role_code : role_codes) {
				SysModAction t = new SysModAction();
				t.setRole_code(role_code);
				sysModActionList.addAll(facade.getSysModActionService().getSysModActionList(t));
			}
		}
		return sysModActionList;
	}

}
